package pool.book;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author Administrator
 * @Date 2020/5/26 14:01
 **/
public class Test {
    private static BookDaoDB dao;
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            System.out.println("----欢迎光临----");
            System.out.println("1.添加图书");
            System.out.println("2.查询所有图书");
            System.out.println("3.更新图书");
            System.out.println("4.删除图书");
            System.out.println("5.退出系统");
            System.out.println("输入你的选择");
            i = sc.nextInt();
            switch (i) {
                case (1):
                    insertUI();
                    break;
                case (2):
                    selectAllUI();
                    break;
                case (3):
                    updateUI();
                    break;
                case (4):
                    deleteUI();
                    break;
                case (5):
                    System.out.println("系统已退出，感谢使用");
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入1-5的整数");
            }

        }
        
    }

    private static void insertUI() {
        System.out.println("添加图书中");
        System.out.println("请输入图书名称");
        sc.nextLine();//处理nextint的回车
        String name=sc.nextLine();

        System.out.println("请输入出版社");
        String press=sc.nextLine();
        System.out.println("请输入作者");
        String author=sc.nextLine();
        System.out.println("请输入价格");
        double price=sc.nextDouble();
        Book book=new Book(name,press,author,price);
        if (dao.insert(book)==-1){
            System.out.println("保存失败，名字重复");
        }else {
            System.out.println("保存成功");
        }

    }

    private static void selectAllUI() {
        System.out.println("查询图书中");
        List<Book> books=dao.selectAll();
        if (books!=null){
            Iterator<Book> iterator=books.iterator();
            while (iterator.hasNext()){
                Book book=iterator.next();
                System.out.println(book);
            }
        }
    }

    private static void updateUI() {
        System.out.println("更新图书中");
        System.out.println("请输入图书名称");
        sc.nextLine();//处理nextint的回车
        String name=sc.nextLine();
        Book book= dao.selectByName(name);
        if (book==null){
            System.out.println("输入的书不存在");
        }else {
            System.out.println("请输入图书名称");
            name=sc.nextLine();
            System.out.println("请输入出版社");
            String press=sc.nextLine();
            System.out.println("请输入作者");
            String author=sc.nextLine();
            System.out.println("请输入价格");
            double price=sc.nextDouble();
            Book book1=new Book(name,press,author,price);
            int i=dao.update(book.getId(),book1);
        }

        double price=sc.nextDouble();
    }

    private static void deleteUI() {
        System.out.println("删除图书中");
        System.out.println("请输入要删除的图书名称");
        sc.nextLine();
        String name=sc.nextLine();
        Book book=dao.selectByName(name);
        if (book==null){
            System.out.println("要删除的图书不存在");
        }else {
            int i=dao.delete(book.getId());
            if (i>0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }
    }
}
